home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / DEFAULTOBSERVABLE.PY < prev    next >
Encoding:
Python Source  |  2000-11-10  |  9.5 KB  |  281 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. __doc__="""Implement Observable interface (see
  65. http://www.zope.org/Members/michel/Projects/Interfaces/ObserverAndNotification)
  66. This class is intended to be used as a mixin (note that it doesn't derive
  67. from any Zope persistence classes, for instance).
  68.  
  69. $Id: DefaultObservable.py,v 1.2.2.1 2000/11/10 16:55:41 tseaver Exp $"""
  70.  
  71. __version__='$Revision: 1.2.2.1 $'[11:-2]
  72.  
  73. import string
  74. from types import StringType
  75.  
  76. class DefaultObservable:
  77.     """
  78.     See the Interfaces wiki for design notes:
  79.  
  80. http://www.zope.org/Members/michel/Projects/Interfaces/ObserverAndNotification
  81.  
  82.     DefaultObservable is intended to be used as a mix-in, like so::
  83.  
  84.         from OFS.SimpleItem import SimpleItem
  85.         from OFS.DefaultObservable import DefaultObservable
  86.  
  87.         class Foo( SimpleItem, DefaultObservable ):
  88.             '''
  89.                 Some foo or other
  90.             '''
  91.             ...
  92.             def bar( self, ... ):
  93.                 '''
  94.                 '''
  95.                 ...
  96.                 self.notify( "bar" )
  97.  
  98.     Clients register with a Foo instance using the methods of the
  99.     Observable interface, e.g.::
  100.  
  101.         foo.registerObserver( self.getPhysicalPath() + ( 'watchFoo',) )
  102.  
  103.     When the Foo instance has its 'bar()' method called, it will
  104.     notify all registered observers, passing 'bar' as the event;  in
  105.     this case, the client's 'watchFoo()' method will be called, with
  106.     the foo object and 'bar' passed as parameters.
  107.     """
  108.  
  109.     def __init__( self, debug=0 ):
  110.         self._observers = []
  111.         self._debug = debug
  112.     
  113.     def _normalize( self, observer ):
  114.  
  115.         # Assert that observer is a string or a sequence of strings.
  116.         if type( observer ) != StringType:
  117.             observer = string.join( observer, '/' ) 
  118.  
  119.         return observer
  120.  
  121.     #
  122.     #   Observable interface methods.
  123.     #
  124.     def registerObserver( self, observer ):
  125.  
  126.         normal = self._normalize( observer )
  127.         if self.restrictedTraverse( normal, None ):
  128.             self._observers.append( normal )
  129.         else:
  130.             raise NameError, observer
  131.  
  132.     def unregisterObserver( self, observer ):
  133.  
  134.         self._observers.remove( self._normalize( observer ) )
  135.  
  136.     #
  137.     #   Convenience method for derivatives.
  138.     #
  139.     def notify( self, event=None ):
  140.  
  141.         bozos = []
  142.  
  143.         for observer in self._observers:
  144.  
  145.             obj = self.restrictedTraverse( observer, None )
  146.  
  147.             if obj is not None:
  148.                 try:
  149.                     obj( self, event )
  150.                 except:
  151.                     bozos.append( observer ) # Veto not allowed!
  152.                     if self._debug:
  153.                         import traceback
  154.                         traceback.print_exc()
  155.             else:
  156.                 bozos.append( observer )
  157.             
  158.         for bozo in bozos:
  159.             try: # avoid race condition if unregister() called before now
  160.                 self._observers.remove( bozo )
  161.             except:
  162.                 pass
  163.         
  164.  
  165. #
  166. #   Unit tests
  167. #
  168.  
  169. if __name__ == '__main__':
  170.  
  171.     class DontGoHere( Exception ): pass
  172.         
  173.     class TestSubject( DefaultObservable ):
  174.  
  175.         def __init__( self, paths ):
  176.             DefaultObservable.__init__( self, 0 )
  177.             self.paths = paths
  178.  
  179.         def restrictedTraverse( self, path, default ):
  180.             return self.paths.get( path, default )
  181.  
  182.     callbacks = {}
  183.  
  184.     def recordCallback( name, subject, event ):
  185.         cbrec = callbacks.get( name, None )
  186.         if cbrec is None:
  187.             cbrec = callbacks[ name ] = []
  188.         cbrec.append( ( subject, event ) )
  189.         
  190.  
  191.     class TestObserver:
  192.  
  193.         def __call__( self, subject, event ):
  194.             recordCallback( 'direct', subject, event )
  195.         
  196.         def namedCallback( self, subject, event ):
  197.             recordCallback( 'named', subject, event )
  198.         
  199.         def named2Callback( self, subject, event ):
  200.             recordCallback( 'named2', subject, event )
  201.         
  202.         def boundCallback( self, subject, event ):
  203.             recordCallback( 'bound', subject, event )
  204.  
  205.     def freefuncObserver( subject, event ):
  206.         recordCallback( 'freefunc', subject, event ) 
  207.     
  208.     def tryVeto( subject, event ):
  209.         """ Simulate attempted veto. """
  210.         raise 'Idawanna!'
  211.     
  212.     observer = TestObserver()
  213.  
  214.     # Simulate Zope's path traversal mechanism.
  215.     paths = {}
  216.     paths[ 'direct'         ] = observer
  217.     paths[ 'direct/named'   ] = observer.namedCallback
  218.     paths[ 'direct/named2'  ] = observer.named2Callback
  219.     paths[ 'bound'          ] = observer.boundCallback
  220.     paths[ 'freefunc'       ] = freefuncObserver
  221.     paths[ 'tryVeto'        ] = tryVeto
  222.  
  223.     subject = TestSubject( paths )
  224.  
  225.     subject.registerObserver( 'direct' )
  226.     subject.registerObserver( 'bound' )
  227.     subject.registerObserver( 'direct/named' )
  228.     subject.registerObserver( ( 'direct', 'named2' ) )
  229.     subject.registerObserver( 'freefunc' )
  230.     subject.registerObserver( 'tryVeto' )
  231.  
  232.     try:
  233.         subject.registerObserver( 'nonesuch' )
  234.         raise DontGoHere( 'path not found' )
  235.     except NameError:
  236.         pass
  237.     except:
  238.         import traceback
  239.         traceback.print_exc()
  240.  
  241.     try:
  242.         subject.registerObserver( 3.1415926 )
  243.         raise DontGoHere( 'non-path' )
  244.     except TypeError:
  245.         pass
  246.     except:
  247.         import traceback
  248.         traceback.print_exc()
  249.  
  250.     subject.notify( 'First event' )
  251.  
  252.     subject.unregisterObserver( 'bound' )
  253.     subject.notify( { 'name' : 'Second event', 'value' : 42 } )
  254.  
  255.     for key in callbacks.keys():
  256.         print '[%s]' % key
  257.         for cb in callbacks[ key ]:
  258.             print '    %s' % `cb`
  259.  
  260.